
const util = require('./util.js');
/*
* post网络请求 
* @param string | object 请求地址
* @param object data POST请求数组
* @param callable successCallback 成功执行方法
* @param callable errorCallback 失败执行方法
*/
const basePost = function (url, data, successCallback, errorCallback, header) {
    if (typeof url == 'object') url = U(url)
    wx.request({
        url: url,
        data: data,
        dataType  : 'json',
        method: 'POST',
        header: header ? header : {
            'content-type': 'application/json',
            'token': wx.getStorageSync('token') ? wx.getStorageSync('token') : '',
            'authToken': wx.getStorageSync('token') ? wx.getStorageSync('token') : '',
            'requestTerminal': 30
        },
        success: function (res) {
            try{
                if (res.data.code == 200 || res.data.state == 200) {
                    successCallback && successCallback(res.data)
                } else {
                    return util.Tips({ title: res.data.message })
                    
                }
            } catch (e) {
                console.log(e)
            }
        }, fail: function (res) {
            errorCallback && errorCallback(res)
            return util.Tips({ title: '连接异常，请稍后重试' })
        },
        complete: function (res) {
        }
    })
}
    /*
    * get网络请求 
    * @param string | object 请求地址
    * @param callable successCallback 成功执行方法
    * @param callable errorCallback 失败执行方法
    * @param boolean isMsg 是否自动提示错误 默认提示
    */
    const baseGet = function (url, successCallback, errorCallback, isMsg, header) {
        if (typeof url == 'object') url = U(url)
        wx.request({
            url: url,
            dataType  : 'json',
            method: 'GET',
            header: header ? header : {
                'content-type': 'application/json',
                'token': wx.getStorageSync('token') ? wx.getStorageSync('token') : '',
                'requestTerminal': 30
            },
            success: function (res) {
                try{
                    if (res.data.code == 200) {
                        successCallback && successCallback(res.data)
                    } else {
                        errorCallback && errorCallback(res.data)
                        isMsg || util.Tips({ title: res.data.msg })
                    }
                } catch (e) {
                    console.log(e)
                }
            }, fail: function (res) {
                console.log('submit fail')
            },
            complete: function (res) {
        
            }
        })
    }

    /*
    * post form传参
    * @param string | object 请求地址
    * @param callable successCallback 成功执行方法
    * @param callable errorCallback 失败执行方法
    * @param boolean isMsg 是否自动提示错误 默认提示
    */
   const baseForm = function (url, data, successCallback, errorCallback, header) {
        if (typeof url == 'object') url = U(url)
        wx.request({
            url: url,
            data: data,
            method: 'POST',
            header: header ? header : {
                'content-type': 'application/x-www-form-urlencoded',
                'token': wx.getStorageSync('token') ? wx.getStorageSync('token') : '',
                'requestTerminal': 30
            },
            success: function (res) {
                try{
                    if (res.data.code == 200) {
                        successCallback && successCallback(res.data)
                    } else {
                        wx.showToast({
                            title: res.data.message,
                            icon: 'none',
                            duration: 1000,
                            mask: true,
                        })
                        errorCallback && errorCallback(res.data)
                    }
                } catch (e) {
                    console.log(e)
                }
            }, fail: function (res) {
                errorCallback && errorCallback(res)
                return util.Tips({ title: '连接异常，请稍后重试' })
            },
            complete: function (res) {
            }
        })
    }

    module.exports = {
        basePost: basePost,
        baseGet: baseGet,
        baseForm: baseForm,
    }
  